import urllib.request
import urllib.parse

def create_request(page):
    url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'}
    data = {
        'cname': '北京',
        'pid':'',
        'pageIndex': page,
        'pageSize': 10
    }
    new_data = urllib.parse.urlencode(data).encode('utf-8')
    request = urllib.request.Request(url=url,data=new_data,headers=headers)
    opens = urllib.request.urlopen(request)
    com = opens.read().decode('utf-8')
    return com

def create_file(page,com):
    df = open('../爬取的文件/kdj_'+str(page)+'.json','w',encoding='utf-8')
    df.write(com)


if __name__ == '__main__':
    start_page = int(input('请输入起始页码:'))
    end_page = int(input('请输入结束页码:'))

    for page in range(start_page,end_page+1):
        com = create_request(page)
        create_file(page,com)